﻿using System.Windows.Input;

namespace KinonekoSoftware.Foundation.UI.Controls.Containers
{
#if WINDOWS
    using System.Windows.Input;
    using System.Windows.Controls.Primitives;


#else
    using Avalonia.Input;
    using Avalonia.Interactivity;

#endif

    public class Island : SectionBase
    {
#if WINDOWS

        public override void OnApplyTemplate()
        {

            if (GetTemplateChild("PART_ToolBar") is not Button button) return;
            button.Click    += OnButtonTapped;
            button.Unloaded += OnButtonUnloaded;
            base.OnApplyTemplate();
        }

        private void OnButtonTapped(object sender, EventArgs e)
        {
            var ctxMenu = ContextMenu;

            if (ctxMenu is null) return;
            ctxMenu.PlacementTarget = (UIElement)sender;
            ctxMenu.Placement       = PlacementMode.Bottom;
            ctxMenu.IsOpen          = true;

        }
        private void OnButtonUnloaded(object sender, RoutedEventArgs e)
        {
            var button = (Button)sender;
            button.Click    -= OnButtonTapped;
            button.Unloaded -= OnButtonUnloaded;
        }

#else
        protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
        {
            var button = e.NameScope.Find<Button>("PART_ToolBar");
            
            if(button is null) return;
            button.Tapped += OnButtonTapped;
            button.Unloaded += OnButtonUnloaded;
            base.OnApplyTemplate(e);
        }
        private void OnButtonTapped(object sender, TappedEventArgs e)
        {
            var ctxMenu = ContextMenu;

            ctxMenu?.Open((Control)sender);
        }
        private void OnButtonUnloaded(object sender, RoutedEventArgs e)
        {
            var button = (Button)sender;
            button.Tapped -= OnButtonTapped;
            button.Unloaded -= OnButtonUnloaded;
        }
#endif
    }



    public class Land : SectionBase, ICommandSource
    {
#if WINDOWS
        public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
            nameof(Command),
            typeof(ICommand),
            typeof(Land),
            new PropertyMetadata(default(ICommand)));


        public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register(
            nameof(CommandParameter),
            typeof(object),
            typeof(Land),
            new PropertyMetadata(default(object)));


        public static readonly DependencyProperty CommandTargetProperty = DependencyProperty.Register(
            nameof(CommandTarget),
            typeof(IInputElement),
            typeof(Land),
            new PropertyMetadata(default(IInputElement)));

        public IInputElement CommandTarget
        {
            get => (IInputElement)GetValue(CommandTargetProperty);
            set => SetValue(CommandTargetProperty, value);
        }

        public object CommandParameter
        {
            get => GetValue(CommandParameterProperty);
            set => SetValue(CommandParameterProperty, value);
        }

        public ICommand Command
        {
            get => (ICommand)GetValue(CommandProperty);
            set => SetValue(CommandProperty, value);
        }

#else
        
        public static readonly StyledProperty<ICommand> CommandProperty          = AvaloniaProperty.Register<Land, ICommand>(nameof(Command));
        public static readonly StyledProperty<object>   CommandParameterProperty = AvaloniaProperty.Register<Land, object>(nameof(CommandParameter));
        public static readonly StyledProperty<IInputElement>   CommandTargetProperty          = AvaloniaProperty.Register<Land, IInputElement>(nameof(CommandTarget));

        public IInputElement CommandTarget
        {
            get => GetValue(CommandTargetProperty);
            set => SetValue(CommandTargetProperty, value);
        }
        
        public object CommandParameter
        {
            get => GetValue(CommandParameterProperty);
            set => SetValue(CommandParameterProperty, value);
        }
        
        public ICommand Command
        {
            get => GetValue(CommandProperty);
            set => SetValue(CommandProperty, value);
        }
        
        public void CanExecuteChanged(object sender, EventArgs e)
        {
            // todo-
        }
#endif
    }
}